/*
 * @Author: 码上talk|RC
 * @Date: 2020-06-09 23:23:29
 * @LastEditTime: 2020-11-23 19:03:38
 * @LastEditors: 码上talk|RC
 * @Description: 
 * @FilePath: /tacomall-admin/src/store/user/actions.js
 * @Just do what I think it is right
 */
/* eslint-disable */
import * as types from './mutation-types'
import api from '@/api'
import { session } from '@/libs/session'
import { router } from '@/router'
import { authRoutes } from '@/router/routes/auth'
import { initMenu } from '@/router/menu/init'
import { authMenu } from '@/router/menu/auth'
var _ = require('lodash')

const genValidMenu = (menu, routes) => {
    let validMenu = []
    for (let i = 0; i < menu.length; i++) {
        if (menu[i].subMenu && menu[i].subMenu.length) {
            const v = genValidMenu(menu[i].subMenu, routes)
            if (v.length) {
                const m = _.cloneDeep(menu[i])
                m.subMenu = v
                validMenu.push(m)
            }
        }
        for (let j = 0; j < routes.length; j++) {
            if (menu[i].routeKey === routes[j].name) {
                validMenu.push(_.cloneDeep(menu[i]))
                break
            }
            break
        }

    }
    return validMenu
}

export const actions = {
    getUserInfo({ commit }) {
        return new Promise((resolve) => {
            api.user.info().then(res => {
                const { status, data } = res
                if (status) {
                    const { role } = data
                    commit(types.SET_PROFILE, data)
                    commit('setIsLogin', true, { root: true })
                    if (role && role.routes.length) {
                        const { routes } = role
                        let validRoutes = [
                            {
                                name: 'auth',
                                path: '/auth',
                                component: () => import('@/layout/auth/index'),
                                children: []
                            }
                        ]
                        for (let i = 0; i < routes.length; i++) {
                            for (let j = 0; j < authRoutes.length; j++) {
                                if (authRoutes[j].name === routes[i].name) {
                                    validRoutes[0].children.push(_.cloneDeep(authRoutes[j]))
                                    break
                                }
                            }
                        }
                        router.addRoutes(validRoutes)
                        commit('setMenu', _.concat(initMenu, genValidMenu(authMenu, routes)), { root: true })
                    }
                    resolve()
                }
            })
        })
    },
    logout({ commit }) {
        return new Promise((resolve) => {
            api.user.info().then(res => {
                if (res.status) {
                    session.exit()
                    commit(types.SET_PROFILE, {})
                    commit('setIsLogin', false, { root: true })
                    resolve()
                }
            })
        })
    }
}
